package com.yanqu.road.dao.impl.union.cross;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.union.cross.CrossUnionReward;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossUnionRewardDaoImpl extends TempDataObjectDao<CrossUnionReward> {

    public int getMaxRecordId() {
        String sql = "select ifnull(max(recordId),0) as maxId from t_u_cross_union_reward";
        ResultSet rs = null;
        CallableStatement call = null;
        int maxId = 0;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                maxId = rs.getInt("maxId");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return maxId;
    }

    public static String getShangBangRewardSql = "select * from `t_u_cross_union_reward` where `sync`=0";
    public Map<Long, CrossUnionReward> getShangBangReward() {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, CrossUnionReward> result = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getShangBangRewardSql);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossUnionReward data = parse(rs);
                result.put(data.getRecordId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }

    public static String insertShangBangRewardSql = "insert into `t_u_cross_union_reward`(recordId,userId,unionUid,reward,type,sync,addTime,syncTime,son) values (?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(CrossUnionReward data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getRecordId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getUnionUid()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getReward()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getType()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getSync()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getAddTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getSyncTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getSon()));
        if (getDb().executeNoneQuery(insertShangBangRewardSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateShangBangRewardSql = "update `t_u_cross_union_reward` set userId=?,unionUid=?,reward=?,type=?,sync=?,addTime=?,syncTime=?,son=? where recordId=?";

    @Override
    protected boolean updateData(CrossUnionReward data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getUnionUid()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getReward()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getType()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getSync()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getAddTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getSyncTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getSon()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getRecordId()));
        if (getDb().executeNoneQuery(updateShangBangRewardSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected CrossUnionReward parseData(ResultSet rs) throws SQLException {
        CrossUnionReward data = new CrossUnionReward();
        data.setRecordId(rs.getLong("recordId"));
        data.setUserId(rs.getLong("userId"));
        data.setUnionUid(rs.getString("unionUid"));
        data.setReward(rs.getString("reward"));
        data.setType(rs.getInt("type"));
        data.setSync(rs.getInt("sync"));
        data.setAddTime(rs.getInt("addTime"));
        data.setSyncTime(rs.getInt("syncTime"));
        data.setSon(rs.getInt("son"));
        return data;
    }
}
